<?php
/**
 * @file bg-top-json.php
 * @version 0.1
 * @desc 输出买入、卖出TOP值, 输出json
 */

$action = $_POST['action'];

if ($action == 'getTop'){
require_once ../libs/function.php;
	$msg = '';

$conn = &ADONewConnection('mysql');
$conn->debug = false;
$conn->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // or die( $conn->ErrorMsg() ); 
if (!$conn){
	$msg = "MySQL Connect Failed!";
	die("Mysql 连接失败！");
}
$conn->Execute('SET NAMES UTF8');


// 卖出
$query4 = "SELECT `price` , `num`
			FROM `bus_top`
			WHERE `flag` = '4'
			ORDER BY price DESC";
$ADODB_FETCH_MODE = ADODB_FETCH_ASdSOC;

$sell = array();
$buy = array();

$re = $conn->Execute($query4);
if (!$re) echo $conn->ErrorMsg(); 
else {
	while (!$re->EOF){
		//echo "<sell><price>{$re->fields['price']}</price><num>{$re->fields['num']}</num></sell>";
		array_push($sell,array('price' => $re->fields['price'],
								'num' => $re->fields['num']));
		$re->MoveNext();
	}
}
$re->Close();


// 买入
$query3 = "SELECT `price` , `num`
			FROM `bus_top`
			WHERE `flag` = '3'
			ORDER BY price DESC";
$ADODB_FETCH_MODE = ADODB_FETCH_ASdSOC;
//$re = $conn->Execute('CALL top(4,5,5 )');
//var_dump($re);
$re = $conn->Execute($query3);
if (!$re) echo $conn->ErrorMsg(); 
else {
	while (!$re->EOF){
		//echo "<buy><price>{$re->fields['price']}</price><num>{$re->fields['num']}</num></buy>";
		array_push($buy,array('price' => $re->fields['price'],
								'num' => $re->fields['num']));
		$re->MoveNext();
	}
}
$re->Close();
$conn->Close();

$result = array('sell' => $sell,
				'buy' => $buy,
				'msg' => $msg);
echo json_encode($result);
} //END if getTop


?>